package com.test.deencredit.bean_deencredit.config_deencredit;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.test.deencredit.R;
import com.test.deencredit.util_deencredit.UserInfoUtil_deencredit;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class AppShowCustomize_deencredit {

    private static class Inner_deencredit {
        private static AppShowCustomize_deencredit instance_deencredit = new AppShowCustomize_deencredit();
    }

    private AppShowCustomize_deencredit() {}

    public static AppShowCustomize_deencredit getInstance_deencredit() {
        return Inner_deencredit.instance_deencredit;
    }

    public static void parse_deencredit(String jsonStr) {

        Inner_deencredit.instance_deencredit = new Gson().fromJson(jsonStr, AppShowCustomize_deencredit.class);

        Inner_deencredit.instance_deencredit.recommendAppList = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);

            JSONArray recommendAppListJsarray= jsonObject.optJSONArray("recommendAppList");
            if (recommendAppListJsarray!=null){
                for (int i=0;i<recommendAppListJsarray.length();i++){
                    JSONObject recommendInfoJsobject = recommendAppListJsarray.optJSONObject(i);
                    if (recommendInfoJsobject!=null){
                        Inner_deencredit.instance_deencredit.recommendAppList.add(RecommendAppInfo_deencredit.getInstance(recommendInfoJsobject.toString()));
                    }
                }
            }

            JSONObject loginInfoJson = jsonObject.optJSONObject("loginInfo");
            if (loginInfoJson != null) Inner_deencredit.instance_deencredit.loginInfo = AppShowLoginInfo_deencredit.getInstance(loginInfoJson.toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }finally {
            Inner_deencredit.instance_deencredit.srcStr = jsonStr;
        }

    }

    //参数
    private String mobile;//客服电话
    private String email;//客服邮箱
    private String customerServiceHint;//客服提示文案
    private String agreementUrl;//协议链接
    private JsonArray accList;//测试账号
    private JsonArray walletChannels;//法语区绑卡渠道列表
    private String rejectPeriod;//借款失败后间隔时间
    private String showSysReview;//评分弹窗控制
    private AppShowLoginInfo_deencredit loginInfo;//登录页信息
    private AppShowPermissions_deencredit permissions;//权限配置
    private String recommendFlag;//app推荐开关（YES/NO）
    private ArrayList<RecommendAppInfo_deencredit> recommendAppList;//app推荐列表

    private String bindAccountTipsTitle;//法语区绑卡页面提示文案标题
    private String bindAccountTipsDesc;//法语区绑卡页面提示文案

    private String srcStr;
    private ArrayList<AccountChannel_Fr_deencredit> accountChannels;

    public String[] getMobiles_deencredit(){
        if (TextUtils.isEmpty(mobile)) return new String[0];
        return mobile.split(",");
    }

    public String[] getEmails_deencredit(){
        if (TextUtils.isEmpty(email)) return new String[0];
        return email.split(",");
    }

    public String getCustomerServiceHint_deencredit() {
        if (TextUtils.isEmpty(customerServiceHint)) return "";
        return customerServiceHint;
    }

    public String getAgreementUrl_deencredit() {
        //tag2024
        if (TextUtils.isEmpty(agreementUrl)) return "";
        Log.w("---agreement---", agreementUrl );
        return agreementUrl;
    }

    public String getTestToken_deencredit(String mobile){
        if (accList == null || accList.size()<1) return "";
        for (int i=0;i<accList.size();i++){
            try {
                JSONObject accJson = new JSONObject(accList.get(i).toString());
                if (accJson.optString("accM").equalsIgnoreCase(mobile)){
                    //tag2024
                    String token = accJson.optString("accTk");
                    Log.w("---test_mobile---", mobile );
                    Log.w("---test_token---", token );
                    return token;
                }
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
        return "";
    }

    public boolean isTestAccount_deencredit(String mobile){
        return !TextUtils.isEmpty(getTestToken_deencredit(mobile));
    }

    public boolean isTestAccount_deencredit(){
        return !TextUtils.isEmpty(getTestToken_deencredit(UserInfoUtil_deencredit.getInstance_deencredit().getMobile_deencredit()));
    }

    private void initAccountChannels_deencredit(){
        accountChannels = new ArrayList<>();
        if (walletChannels==null || walletChannels.isEmpty()) return;
        Gson gson = new Gson();
        for (int i=0;i<walletChannels.size();i++){
            AccountChannel_Fr_deencredit channel = gson.fromJson(walletChannels.get(i).toString(), AccountChannel_Fr_deencredit.class);
            accountChannels.add(channel);
        }
    }

    public ArrayList<AccountChannel_Fr_deencredit> getAccountChannels_deencredit(){
        if (!hasAccountChannel_deencredit()) initAccountChannels_deencredit();
        return accountChannels;
    }

    public Bitmap getChannelLogoByDesc_deencredit(String channelDesc){
        if (!hasAccountChannel_deencredit()) initAccountChannels_deencredit();
        for (AccountChannel_Fr_deencredit channel:accountChannels){
            if (channelDesc.equalsIgnoreCase(channel.getDesc_deencredit())) return channel.getBitmap_deencredit();
        }
        return null;
    }

    public Bitmap getChannelLogoByValue_deencredit(String channelValue){
        if (!hasAccountChannel_deencredit()) initAccountChannels_deencredit();
        for (AccountChannel_Fr_deencredit channel:accountChannels){
            if (channelValue.equalsIgnoreCase(channel.getValue_deencredit())) return channel.getBitmap_deencredit();
        }
        return null;
    }

    public void setChannelLogoByDesc_deencredit(String desc, Bitmap logoBm){
        if (!hasAccountChannel_deencredit()) initAccountChannels_deencredit();
        for (AccountChannel_Fr_deencredit channel:accountChannels){
            if (desc.equalsIgnoreCase(channel.getDesc_deencredit())) channel.setBitmap_deencredit(logoBm);
        }
    }

    public void setChannelLogoByValue_deencredit(String value, Bitmap logoBm){
        if (!hasAccountChannel_deencredit()) initAccountChannels_deencredit();
        for (AccountChannel_Fr_deencredit channel:accountChannels){
            if (value.equalsIgnoreCase(channel.getValue_deencredit())) channel.setBitmap_deencredit(logoBm);
        }
    }

    public boolean hasAccountChannel_deencredit(){
        return accountChannels!=null && !accountChannels.isEmpty();
    }

    public String getRejectPeriod_deencredit(Context context) {
        if (TextUtils.isEmpty(rejectPeriod)) return context.getString(R.string.days_deencredit,7);
        try {

            return context.getString(R.string.days_deencredit,Integer.parseInt(rejectPeriod));
        }catch (NumberFormatException e){
            e.printStackTrace();
            return context.getString(R.string.days_deencredit,7);
        }
    }

    public String[] getHomePermission_deencredit(){
        if (permissions == null) return new ArrayList<String>().toArray(new String[0]);
        ArrayList<String> home = permissions.getHome_deencredit();
        return home.toArray(new String[0]);
    }

    public boolean getRecommendFlag_deencredit(){
        if (TextUtils.isEmpty(recommendFlag)) return false;
        return recommendFlag.equalsIgnoreCase("YES");
    }

    public ArrayList<RecommendAppInfo_deencredit> getRecommendAppList_deencredit() {
        if (recommendAppList == null) new ArrayList<>();
        return recommendAppList;
    }

    public boolean showSysReview_deencredit(){
        if (TextUtils.isEmpty(showSysReview)) return false;
        return showSysReview.equalsIgnoreCase("YES");
    }


    public String getLoginAmountGranularity_deencredit() {
        if (loginInfo == null) return "";
        return loginInfo.getAmountGranularity_deencredit();
    }

    public String getLoginMinAmount_deencredit() {
        if (loginInfo == null) return "";
        return loginInfo.getMinAmount_deencredit();
    }

    public String getLoginMaxAmount_deencredit() {
        if (loginInfo == null) return "";
        return loginInfo.getMaxAmount_deencredit();
    }

    public String getLoginInterestRate_deencredit() {
        if (loginInfo == null) return "";
        return loginInfo.getInterestRate_deencredit();
    }

    public String getLoginInterestRateCycle_deencredit() {
        if (loginInfo == null) return "";
        return loginInfo.getInterestRateCycle_deencredit();
    }

    public String getBindAccountTipsTitle_deencredit() {
        if (TextUtils.isEmpty(bindAccountTipsTitle)) return "";
        return bindAccountTipsTitle;
    }

    public String getBindAccountTipsDesc_deencredit() {
        if (TextUtils.isEmpty(bindAccountTipsDesc)) return "";
        return bindAccountTipsDesc;
    }

    public String getOther_deencredit(String pName){
        if (TextUtils.isEmpty(srcStr)) return "";

        try {
            JSONObject jsonObject = new JSONObject(srcStr);
            String result = jsonObject.optString(pName);
            if (TextUtils.isEmpty(result)) return "";
            return result;
        } catch (JSONException e) {
            e.printStackTrace();
            return "";
        }

    }

}
